/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package minemapgenerator;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Image;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import util.GameUtility;

/**
 *
 * @author CodeBlue
 */
public class MineMapGenerator extends javax.swing.JFrame {

    /**
     * Creates new form MineMapGenerator
     */
    public MineMapGenerator() {
        initComponents();
        Image icon = getToolkit().getImage(getClass().getResource("/images/icon.png"));
        setIconImage(icon);
        setLocationRelativeTo(null);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        btnGen = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        mnNew = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        mnAbout = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Mine Map Generator");

        mainPanel.setBackground(new java.awt.Color(255, 255, 255));
        mainPanel.setLayout(new java.awt.BorderLayout());

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(153, 0, 0));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Mine Map Generator");
        jLabel1.setMaximumSize(new java.awt.Dimension(184, 50));
        jLabel1.setMinimumSize(new java.awt.Dimension(184, 50));
        jLabel1.setPreferredSize(new java.awt.Dimension(184, 50));
        mainPanel.add(jLabel1, java.awt.BorderLayout.PAGE_START);

        btnGen.setText("Generate");
        btnGen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGenActionPerformed(evt);
            }
        });
        mainPanel.add(btnGen, java.awt.BorderLayout.PAGE_END);

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(51, 51, 255));
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setText("Click File -> New to create a new map");
        jLabel2.setMaximumSize(new java.awt.Dimension(343, 200));
        jLabel2.setMinimumSize(new java.awt.Dimension(343, 200));
        jLabel2.setPreferredSize(new java.awt.Dimension(343, 200));
        mainPanel.add(jLabel2, java.awt.BorderLayout.CENTER);

        getContentPane().add(mainPanel, java.awt.BorderLayout.CENTER);

        jMenu1.setText("File");

        mnNew.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK));
        mnNew.setText("New");
        mnNew.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnNewActionPerformed(evt);
            }
        });
        jMenu1.add(mnNew);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("About");

        mnAbout.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F1, 0));
        mnAbout.setText("About");
        mnAbout.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnAboutActionPerformed(evt);
            }
        });
        jMenu2.add(mnAbout);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnGenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGenActionPerformed
        // TODO add your handling code here:
        Component centerComp = mainPanel.getComponent(2);
        // check if it's ready to generate a map
        if (centerComp instanceof JLabel) {
            JOptionPane.showMessageDialog(this, "Click File -> New to start!", "Notification", JOptionPane.CLOSED_OPTION);
            return;
        }
        JPanel mapPanel = (JPanel) centerComp;
        Component[] components = mapPanel.getComponents();
        for (Component component : components) {
            MyButton myButton = (MyButton) component;
            myButton.setEnabled(false);
            if (myButton.getBombArround() == -1) {
                continue;
            }
            int x = myButton.getPoint().x;
            int y = myButton.getPoint().y;
            int bombs = GameUtility.getBombArround(components, x, y);
            myButton.setBombArround(bombs);
            myButton.setText(bombs + "");
        }
    }//GEN-LAST:event_btnGenActionPerformed

    private void mnNewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnNewActionPerformed
        // TODO add your handling code here:
        NewMap newMap = new NewMap(this, true);
        newMap.setVisible(true);
        if (newMap.isCancel) {
            return;
        }
        mainPanel.remove(2);
        mainPanel.add(new GenPanel(), BorderLayout.CENTER);
        pack();
    }//GEN-LAST:event_mnNewActionPerformed

    private void mnAboutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnAboutActionPerformed
        // TODO add your handling code here:
        new About(this, true).setVisible(true);
    }//GEN-LAST:event_mnAboutActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Windows look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Windows".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MineMapGenerator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MineMapGenerator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MineMapGenerator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MineMapGenerator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new MineMapGenerator().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnGen;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JMenuItem mnAbout;
    private javax.swing.JMenuItem mnNew;
    // End of variables declaration//GEN-END:variables
}
